**respondent marginal means
clear all
use new_data.dta
**MM by education level: uni vs primary school

*primary school unconstrained attributes
**no need for interaction terms for margins of non constrained attributes
   global mod_educ_primary = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if educ_new==1" 
   qui: reg outcome $mod_educ_primary, $ses
   margins sex  religion  residence occupation education language friends team_support children, coeflegend post 
   est store educ_primary1
   
*highschool unconstrained attributes
     global mod_educ_high = "i.sex  i.religion  i.residence ib4.occupation i.education i.language ib3.friends ib2.team_support ib3.children  i.country i.legal_status [pweight=weight] if educ_new==2" 
   qui: reg outcome $mod_educ_high, $ses
   margins sex  religion  residence occupation education language friends team_support children, coeflegend post 
   est store educ_high1
   
*uni unconstrained attributes
     global mod_educ_uni = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if educ_new==3" 
   qui: reg outcome $mod_educ_uni, $ses
   margins sex  religion  residence occupation education language friends team_support children, coeflegend post 
   est store educ_uni1

*Primary-country MMs
*difference in MM country, drop irregular status because of restriction with Romania
	  
       reg outcome i.country if legal_status==2 & educ_new ==1 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store educ_primary2
	   
*high-country MMs
       reg outcome i.country if legal_status==2 & educ_new ==2 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store educ_high2
	   
*uni-country MMs
       reg outcome i.country if legal_status==2 & educ_new ==3 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store educ_uni2

*primary-legal status MMs
*difference in MM legal status, drop romania because of restriction with irregular
       drop if country==3	  
       reg outcome i.legal_status if educ_new==1 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store educ_primary3
	   
*high- legal status MMs	  
       reg outcome i.legal_status if educ_new==2 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store educ_high3
	   
*uni- legal status MMs	  
       reg outcome i.legal_status if educ_new==3 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store educ_uni3

*graph only 2 groups
coefplot       (educ_primary1, offset(0) msymbol(s) mfcolor(purple) color(purple)  ciopts(color(purple)) ) ///	
               (educ_uni1, offset(0) msymbol(s) mfcolor(green) color(green)  ciopts(color(green)) ) ///	          
			   (educ_primary2, offset(0) msymbol(s) mfcolor(purple)  color(purple)  ciopts(color(purple))) ///			   			   
			   (educ_uni2, offset(0) msymbol(s) mfcolor(green)  color(green)  ciopts(color(green))) ///	
			   (educ_primary3,  offset(0) msymbol(s)  msymbol(s) mfcolor(purple)  color(purple) ciopts(color(purple))) ///
			   (educ_uni3,  offset(0) msymbol(s)  msymbol(s) mfcolor(green)  color(green) ciopts(color(green))), ///
			    headings(1.sex = "{bf: Child gender}" 1.residence = "{bf: Parents' length of residence}" 1.occupation = "{bf: Parents' occupation}"  ///
1.education = "{bf: Parents' education}" 1.religion = "{bf: Parents' religion}" 1.language = "{bf: Parents' Italian proficiency}" 1.friends = "{bf: Family friends Origin}" 1.children = "{bf:Total number of children}" 1.team_support = "{bf:Parents' team support}" 1.country = "{bf: Parents' country of origin}" 1.legal_status = "{bf:Parents' legal status}") ///
			   xtitle(Marginal Means) ysize(10) xsize(8) xtitle("Marginal Means (percentage)", size(small)) ///
			   xlabel( 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70" 0.8 "80", valuelabel)xlabel(,labsize(vsmall)) ///
			   coeflabels(,labsize(vsmall)) legend(order(2 "Primary" 4 "University")on) graphregion(margin(zero)) graphregion(color(white))

*all three groups			   
coefplot       (educ_primary1, offset(0) msymbol(s) mfcolor(purple) color(purple)  ciopts(color(purple)) ) ///	
               (educ_uni1, offset(0) msymbol(s) mfcolor(green) color(green)  ciopts(color(green)) ) ///
			   (educ_high1, offset(0) msymbol(s) mfcolor(orange) color(orange)  ciopts(color(orange)) ) ///
			   (educ_primary2, offset(0) msymbol(s) mfcolor(purple)  color(purple)  ciopts(color(purple))) ///			   			   
			   (educ_uni2, offset(0) msymbol(s) mfcolor(green)  color(green)  ciopts(color(green))) ///	
			   (educ_high2, offset(0) msymbol(s) mfcolor(orange)  color(orange)  ciopts(color(orange))) ///
			   (educ_primary3,  offset(0) msymbol(s)  msymbol(s) mfcolor(purple)  color(purple) ciopts(color(purple))) ///
			   (educ_uni3,  offset(0) msymbol(s)  msymbol(s) mfcolor(green)  color(green) ciopts(color(green))) ///
			   (educ_high3,  offset(0) msymbol(s)  msymbol(s) mfcolor(orange)  color(orange) ciopts(color(orange))) , ///
			    headings(1.sex = "{bf: Child gender}" 1.residence = "{bf: Parents' length of residence}" 1.occupation = "{bf: Parents' occupation}"  ///
1.education = "{bf: Parents' education}" 1.religion = "{bf: Parents' religion}" 1.language = "{bf: Parents' Italian proficiency}" 1.friends = "{bf: Family friends Origin}" 1.children = "{bf:Total number of children}" 1.team_support = "{bf:Parents' team support}" 1.country = "{bf: Parents' country of origin}" 1.legal_status = "{bf:Parents' legal status}") ///
			   xtitle(Marginal Means) ysize(10) xsize(8) xtitle("Marginal Means (percentage)", size(small)) ///
			   xlabel( 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70" 0.8 "80", valuelabel)xlabel(,labsize(vsmall)) ///
			   coeflabels(,labsize(vsmall)) legend(order(2 "Primary" 4 "University" 6 "High-school" )on) graphregion(margin(zero)) graphregion(color(white)) ///
				order(*.occupation 1.legal_status 2.legal_status  *.residence  *.religion  *.language *.education *.friends  *.team_support *.children 1.country 2.country 3.country 4.country 5.country *.sex)
			   graph export "FigS8_mm-educ.png", width(1000) replace
			   graph export "FigS8_mm-educ.eps", replace
			   
			   

**---------------------------------------------------------------------------------------------------------------------------------------------------
**MMs by voting


clear all
  use new_data.dta
**MM by voting (general elections): five star

*5star unconstrained attributes
**no need for interaction terms for margins of non constrained attributes
   global mod_fivestar = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if voting_18==1" 
   qui: reg outcome $mod_fivestar, $ses
   margins occupation, coeflegend post 
   est store fivestar1
   
   qui: reg outcome $mod_fivestar, $ses
   margins residence  religion  language education friends  team_support children sex, coeflegend post 
   est store fivestar1_b
   
*right-wing for unconstrained attributes
     global mod_right = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if voting_18==2" 
   qui: reg outcome $mod_right, $ses
   margins occupation, coeflegend post 
   est store right1
   
   qui: reg outcome $mod_right, $ses
   margins residence  religion  language education friends  team_support children sex, coeflegend post 
   est store right1_b
   
*left-wing for unconstrained attributes
     global mod_left = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if voting_18==3" 
   qui: reg outcome $mod_left, $ses
   margins occupation, coeflegend post 
   est store left1
   
   qui: reg outcome $mod_left, $ses
   margins residence  religion  language education friends  team_support children sex, coeflegend post 
   est store left1_b
   
*Other for unconstrained attributes
     global mod_other = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if voting_18==4" 
   qui: reg outcome $mod_other, $ses
   margins occupation, coeflegend post 
   est store other1
   
   qui: reg outcome $mod_other, $ses
   margins residence  religion  language education friends  team_support children sex, coeflegend post 
   est store other1_b   

*5star-country MMs
*difference in MM country, drop irregular status because of restriction with Romania
	  
       reg outcome i.country if legal_status==2 & voting_18 ==1 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store fivestar2

*right-country MMs
       reg outcome i.country if legal_status==2 & voting_18 ==2 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store right2
	   
*left-country MMs
       reg outcome i.country if legal_status==2 & voting_18 ==3 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store left2
	   
*other-country MMs
       reg outcome i.country if legal_status==2 & voting_18 ==4 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store other2	   

*fivestar-legal status MMs
*difference in MM legal status, drop romania because of restriction with irregular
       drop if country==3	  
       reg outcome i.legal_status if voting_18==1 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store fivestar3
	   
*right- legal status MMs	  
       reg outcome i.legal_status if voting_18==2 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store right3
	   
*left- legal status MMs	  
       reg outcome i.legal_status if voting_18==3 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store left3
	   
*other- legal status MMs	  
       reg outcome i.legal_status if voting_18==4 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store other3	   

*graph
coefplot       (fivestar1, offset(0) msymbol(s) mfcolor(red) color(red)  ciopts(color(red)) ) ///	
               (right1, offset(0) msymbol(s) mfcolor(green) color(green)  ciopts(color(green)) ) ///
			   (left1, offset(0) msymbol(s) mfcolor(orange) color(orange)  ciopts(color(orange)) ) ///	
			   (other1, offset(0) msymbol(s) mfcolor(navy) color(navy)  ciopts(color(navy)) ) ///
			   			   (fivestar3,  offset(0) msymbol(s)  msymbol(s) mfcolor(red)  color(red) ciopts(color(red))) ///
			   (right3,  offset(0) msymbol(s)  msymbol(s) mfcolor(green)  color(green) ciopts(color(green))) ///
			   (left3,  offset(0) msymbol(s)  msymbol(s) mfcolor(orange)  color(orange) ciopts(color(orange))) ///
			   (other3,  offset(0) msymbol(s)  msymbol(s) mfcolor(navy)  color(navy) ciopts(color(navy))) ///
			   (fivestar1_b, offset(0) msymbol(s) mfcolor(red) color(red)  ciopts(color(red)) ) ///	
               (right1_b, offset(0) msymbol(s) mfcolor(green) color(green)  ciopts(color(green)) ) ///
			   (left1_b, offset(0) msymbol(s) mfcolor(orange) color(orange)  ciopts(color(orange)) ) ///
			   (other1_b, offset(0) msymbol(s) mfcolor(navy) color(navy)  ciopts(color(navy)) ) ///
			   (right2, offset(0) msymbol(s) mfcolor(green)  color(green)  ciopts(color(green))) ///	
			   (left2, offset(0) msymbol(s) mfcolor(orange)  color(orange)  ciopts(color(orange))) ///
			   (fivestar2, offset(0) msymbol(s) mfcolor(red)  color(red)  ciopts(color(red))) ///
			   (other2, offset(0) msymbol(s) mfcolor(navy)  color(navy)  ciopts(color(navy))), ///
			    headings(1.sex = "{bf: Child gender}" 1.residence = "{bf: Parents' length of residence}" 1.occupation = "{bf: Parents' occupation}"  ///
1.education = "{bf: Parents' education}" 1.religion = "{bf: Parents' religion}" 1.language = "{bf: Parents' Italian proficiency}" 1.friends = "{bf: Family friends Origin}" 1.children = "{bf:Total number of children}" 1.team_support = "{bf:Parents' team support}" 1.country = "{bf: Parents' country of origin}" 1.legal_status = "{bf:Parents' legal status}") ///
			   xtitle(Marginal Means) ysize(10) xsize(8) xtitle("Marginal Means (percentage)", size(small)) ///
			   xlabel( 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70" 0.8 "80", valuelabel)xlabel(,labsize(vsmall)) ///
				order(*.occupation 1.legal_status 2.legal_status  *.residence  *.religion  *.language *.education *.friends  *.team_support *.children 1.country 2.country 3.country 4.country 5.country *.sex) ///
			   coeflabels(,labsize(vsmall)) legend(order(2 "FiveStar" 4 "Right-wing" 6 "Left-wing" 8 "Other")on) graphregion(margin(zero)) graphregion(color(white)) 
			   graph export "Fig2_mm-voting.png", width(1000) replace
			   graph export "Fig2_mm-voting.eps", replace
			   
*-----------------------------------------------------------------------------------------------------------------------------------------------
*MM by age groups
clear all
use new_data.dta

*young unconstrained attributes
**no need for interaction terms for margins of non constrained attributes
   global mod_young = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if age_new==3" 
   qui: reg outcome $mod_young, $ses
   margins sex  religion  residence occupation education language friends team_support children, coeflegend post 
   est store young1
   
*middle for unconstrained attributes
     global mod_middle = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if age_new==2" 
   qui: reg outcome $mod_middle, $ses
   margins sex  religion  residence occupation education language friends team_support children, coeflegend post 
   est store middle1
   
*old for unconstrained attributes
     global mod_old = "i.sex  i.religion  i.residence i.occupation i.education i.language i.friends i.team_support i.children  i.country i.legal_status [pweight=weight] if age_new==1" 
   qui: reg outcome $mod_old, $ses
   margins sex  religion  residence occupation education language friends team_support children, coeflegend post 
   est store old1

*old-country MMs
*difference in MM country, drop irregular status because of restriction with Romania
	  
       reg outcome i.country if legal_status==2 & age_new ==3 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store young2

*middle-country MMs
       reg outcome i.country if legal_status==2 & age_new ==2 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store middle2
	   
*young-country MMs
       reg outcome i.country if legal_status==2 & age_new ==1 [pweight=weight], $ses  
	   margins country, coeflegend  post
	   est store old2

*young-legal status MMs
*difference in MM legal status, drop romania because of restriction with irregular
       drop if country==3	  
       reg outcome i.legal_status if age_new==3 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store young3
	   
*middle- legal status MMs	  
       reg outcome i.legal_status if age_new==2 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store middle3
	   
*old- legal status MMs	  
       reg outcome i.legal_status if age_new==1 [pweight=weight], $ses  
	   margins legal_status, coeflegend  post
	   est store old3

*graph
coefplot       (young1, offset(0) msymbol(s) mfcolor(purple) color(purple)  ciopts(color(purple)) ) ///	
               (middle1, offset(0) msymbol(s) mfcolor(green) color(green)  ciopts(color(green)) ) ///
			   (old1, offset(0) msymbol(s) mfcolor(orange) color(orange)  ciopts(color(orange)) ) ///
			   (young2, offset(0) msymbol(s) mfcolor(purple)  color(purple)  ciopts(color(purple))) ///			   			   
			   (middle2, offset(0) msymbol(s) mfcolor(green)  color(green)  ciopts(color(green))) ///	
			   (old2, offset(0) msymbol(s) mfcolor(orange)  color(orange)  ciopts(color(orange))) ///
			   (young3,  offset(0) msymbol(s)  msymbol(s) mfcolor(purple)  color(purple) ciopts(color(purple))) ///
			   (middle3,  offset(0) msymbol(s)  msymbol(s) mfcolor(green)  color(green) ciopts(color(green))) ///
			   (old3,  offset(0) msymbol(s)  msymbol(s) mfcolor(orange)  color(orange) ciopts(color(orange))) , ///
			    headings(1.sex = "{bf: Child gender}" 1.residence = "{bf: Parents' length of residence}" 1.occupation = "{bf: Parents' occupation}"  ///
1.education = "{bf: Parents' education}" 1.religion = "{bf: Parents' religion}" 1.language = "{bf: Parents' Italian proficiency}" 1.friends = "{bf: Family friends Origin}" 1.children = "{bf:Total number of children}" 1.team_support = "{bf:Parents' team support}" 1.country = "{bf: Parents' country of origin}" 1.legal_status = "{bf:Parents' legal status}") ///
			   xtitle(Marginal Means) ysize(10) xsize(8) xtitle("Marginal Means (percentage)", size(small)) ///
			   xlabel( 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70" 0.8 "80", valuelabel)xlabel(,labsize(vsmall)) ///
			   			   			   order(*.occupation 1.legal_status 2.legal_status  *.residence  *.religion  *.language *.education *.friends  *.team_support *.children 1.country 2.country 3.country 4.country 5.country *.sex) ///
			   coeflabels(,labsize(vsmall)) legend(order(2 "18-34" 4 "35-54" 6 "55+")on) graphregion(margin(zero)) graphregion(color(white))
			   graph export "FigS9_mm-age.png", width(1000) replace	
			   graph export "FigS9_mm-age.eps", replace	
